import { Role } from '@/types/role';
import apiClient from '../apiClient';

import { UserInfo, UserToken } from '@/types/entity';
import { TeamMemberItemType } from '@/types/user/team/type';

export interface SignInReq {
  username: string;
  password: string;
}
export interface UserInfoReq {
  id: string;
  userName: string;
  avatar: string;
  identityId: string;
  groupName: string;
  groupId: string;
  roleList: string[];
}

export interface UserCreateReq {
  name: string;
  password: string;
}

export interface passwordUpdateReq {
  origin_password: string;
  new_password: string;
}

export interface UserFetchReq {
  keywords?: string;
  page?: number;
  page_size?: number;
}

export interface AuthCxReq {
  uid: string;
  d: string;
  classid: string;
  courseid: string;
  type: string;
  appid: string;
}

export interface AuthCxRes {
  username: string;
  role: string;
}

interface PermissionsItem {
  [key: string]: string[];
}

export interface updatePermissionsReq {
  policies: PermissionsItem[];
}

export interface updateRolePermissionsReq {
  permissions: PermissionsItem[];
}

export interface updateRoleDebugReq {
  id: string;
}

export interface LicenseReq {
  license_cotent: string;
}

export interface SystemAccountConfigRes {
  allowLocalAccount: boolean;
}

export interface SignUpReq extends SignInReq {
  email: string;
}
export type SignInRes = UserToken & { user: UserInfo; is_first_login?: boolean };

export interface TgUserRegisterReq {
  email: string;
  user_id: string;
  group_name: string;
  password: string;
  // company_name: string;
  // company_website: string;
  name: string;
  rolecode?: string;
  phone?: string;
}

export interface TgUserRegisterRes {
  message: string;
}

export interface AddRoleReq {
  name: string;
  description: string;
  parent_id?: string | undefined;
}

export enum UserApi {
  // SignIn = '/auth/signin',
  SignIn = '/openapi/v1/user/loginByPassword',
  // SignIn = '/ruigpt/help/user/account/loginByPassword',
  // AuthCx = '/openapi/v1/user/cx/sso/login',
  AuthCx = '/openapi/v1/user/cx/sso/login_create',
  SignUp = '/auth/signup',
  Logout = '/auth/logout',
  Refresh = '/auth/refresh',
  Permissions = '/openapi/v1/permissions',
  // Role = '/openapi/v1/role',
  Role = '/openapi/v2/roles',
  FetchRoleApi = '/openapi/v1/roles',
  UpdateRoleDebug = '/openapi/v1/debug/switch_role',
  // RolePermissions = '/openapi/v1/role_permissions',
  // RolePermissions = '/openapi/v/role',
  RolePermissions = '/openapi/v2/roles',

  LicenseApi = '/agent/api/v1/license',
  LicenseRedeemApi = '/agent/api/v1/redeem_license_url',
  // 用户管理
  RuigptUserApi = '/openapi/v1/user',

  // team member
  GetTeamMembers = '/api/proApi/support/user/team/member/list',

  // 用户信息
  // GetUserInfo = '/openapi/v2/users/userinfo',
  GetUserInfo = '/openapi/v2/users',
  UpdateUserInfo = '/openapi/v2/users/userinfo/update',

  // 系统账户配置
  SystemAccountConfig = '/openapi/v2/system_configs/account',
  SystemAccountConfigUpdate = '/openapi/v2/system_configs/account/update',

  // 天工用户注册
  TgUserRegister = '/openapi/v1/user/register',
}

const signin = (data: SignInReq) => apiClient.post<SignInRes>({ url: UserApi.SignIn, data });
const authCx = (data: AuthCxReq) => apiClient.post<AuthCxRes>({ url: UserApi.AuthCx, data });
const signup = (data: SignUpReq) => apiClient.post<SignInRes>({ url: UserApi.SignUp, data });
const logout = () => apiClient.get({ url: UserApi.Logout });
const findById = (id: string) => apiClient.get<UserInfo[]>({ url: `${UserApi.User}/${id}` });
const permissions = () => apiClient.get({ url: UserApi.Permissions });
// const updatePermissions = () => apiClient.put({ url: UserApi.Permissions });
const updatePermissions = (data: updatePermissionsReq) =>
  apiClient.put({ url: UserApi.Permissions, data });

const addRole = (data: AddRoleReq) => apiClient.post<Role>({ url: UserApi.Role, data });
const deleteRole = (id: string) => apiClient.delete({ url: UserApi.Role + `/${id}` });

// 获取角色列表，包括conditions和permissions
const getRole = () => apiClient.get<Role[]>({ url: UserApi.Role });

// const getRoles = () => apiClient.get({ url: UserApi.UpdateRole});
const getRoles = () => apiClient.get({ url: UserApi.FetchRoleApi + '?applicationId=999' });

const updateRoleDebug = (data: updateRoleDebugReq) =>
  apiClient.put({ url: UserApi.UpdateRoleDebug, data });

// const getRolePermissions = () => apiClient.get({ url: UserApi.RolePermissions });
const getRolePermissions = (role_id = '') => {
  let url: string = UserApi.RolePermissions;
  if (role_id) {
    url += `?role_id=${role_id}`;
  }
  return apiClient.get({ url });
};

const updateRolePermissions = (id: string, data: updateRolePermissionsReq) =>
  apiClient.put({ url: UserApi.RolePermissions + `/${id}/permissions`, data });

// License 导入和信息获取
const fetchLicense = () => apiClient.get({ url: UserApi.LicenseApi });
const fetchLicenseUrl = () => apiClient.get({ url: UserApi.LicenseRedeemApi });
const updateLicense = (data: LicenseReq) => apiClient.post({ url: UserApi.LicenseApi, data });

// 用户管理
const createUser = (data: UserCreateReq) => apiClient.post({ url: UserApi.RuigptUserApi, data });
const updatePassword = (userId: string, data: passwordUpdateReq) =>
  apiClient.put({ url: UserApi.RuigptUserApi + `/${userId}`, data });
const fetchUser = (data: UserFetchReq) =>
  apiClient.get({ url: UserApi.RuigptUserApi, params: data });
const deleteUser = (id: string) => apiClient.delete({ url: UserApi.RuigptUserApi + `/${id}` });

const updateRoleType = (id: string) =>
  apiClient.post({ url: UserApi.RolePermissions + `/${id}` + '/subadmin' });

export const getTeamMembers = () =>
  apiClient.get<TeamMemberItemType[]>({ url: UserApi.GetTeamMembers });

export const getUserInfo = (id: string) => apiClient.get<UserInfoReq[]>({ url: UserApi.GetUserInfo  + `/${id}`});

export const updateUserInfo = (id: string, avatar: string) =>
  apiClient.post<UserInfoReq[]>({ url: UserApi.UpdateUserInfo, data: { id, avatar } });

// 获取系统账户配置
export const getSystemAccountConfig = () =>
  apiClient.get<SystemAccountConfigRes>({ url: UserApi.SystemAccountConfig });

export const updateSystemAccountConfig = (data: SystemAccountConfigRes) =>
  apiClient.post<SystemAccountConfigRes>({ url: UserApi.SystemAccountConfigUpdate, data });

// 天工用户注册
export const tgUserRegister = (data: TgUserRegisterReq) =>
  apiClient.post<TgUserRegisterRes>({ url: UserApi.TgUserRegister, data });

export default {
  signin,
  signup,
  findById,
  logout,
  authCx,
  permissions,
  updatePermissions,
  deleteRole,
  addRole,
  getRole,
  getRoles,
  updateRoleDebug,
  getRolePermissions,
  updateRolePermissions,

  createUser,
  updatePassword,
  fetchUser,
  deleteUser,

  fetchLicense,
  fetchLicenseUrl,
  updateLicense,
  updateRoleType,
  getTeamMembers,
  getUserInfo,
  updateUserInfo,
  getSystemAccountConfig,
  updateSystemAccountConfig,
};
